<?php

namespace App\Console\Commands\Kfos;

use App\Console\Commands\OpenShop\DelOrder;
use App\Library\Rms\RmsShopApi;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\SiteLocation\GuanyuanDataService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqPublisher;

class blankOutKfosStore extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'kfos:blank-out-kfos-store';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '关闭kfos订货门店';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws GuzzleException
     * @throws \Exception
     */
    public function handle()
    {
        try {
            GuanyuanDataService::setTimeOut(30);
            $kfShop  = GuanyuanDataService::newShopList();
        } catch (\Exception $exception) {
            $kfShop = [];
        }
        $shopIds = [];
        collect($kfShop)->where('状态', '闭店')->chunk(20)->each(function ($shops) use (&$shopIds) {
            $shopList = (new RmsShopApi())->getShopByCodes(array_column($shops->toArray(), '铺位号'))['data']['shopList'] ?? [];
            foreach ($shopList as $shop) {
                $shopIds[] = $shop['id'];
            }
        });

        Log::info('已闭店的门店统计:数量-' . count($shopIds) . '   详情-'. json_encode($shopIds));

        foreach ($shopIds as $k => $shopId) {
            $this->info('NSQ_NUMBER_' . $k);
            (new NsqPublisher(config('nsq.default.address')))
                ->pub('tiger.openShop.shopStatus.switch', [
                    'nsqEventTime'  => time(),
                    'shopId'        => $shopId,
                    'switch'        => 0, // 门店禁用关闭为 0
                    'operatorId'    => 0,
                    'operatorName'  => '系统',
                    'reason'        => '同步已闭店门店，将订货门店闭店',
                    'operationTime' => time()
                ]);
        }

    }
}
